package assignment10;

import java.io.*;

public class PerFileCopy {
	public static void main(String[] args) {
		//命令行参数个数小于2直接退出
		if(args.length < 2) {
			System.out.println("Please enter source file and dest file");
			return;
		}
		//创建字节处理流对象
		DataInputStream inputStream;
		DataOutputStream outputStream;
		//创建File类对象
		File SourceFile = new File(args[0]);
		File DestFile = new File(args[1]);
		//若源文件不存在则报错
		if(!SourceFile.exists()) {
			System.out.println("File "+args[0]+" does not exists");
			return;
		}
		//若目标文件已存在则报错
		if(DestFile.exists()) {
			System.out.println("File "+args[1]+" already exists");
			return;
		}
		try {
			//创建文件读写流对象, 使用缓冲器处理
			inputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(SourceFile)));
			outputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(DestFile)));
			try {
				int data;
				//死循环, 出现异常则跳出循环
				while(true) {
					data = inputStream.readByte();
					outputStream.writeByte(data);
				}
			} catch (Exception e) { //捕获到异常说明读完了, 此时需要关闭流
				// TODO: handle exception
				inputStream.close();
				outputStream.close();
			}
		} catch (IOException e) { //捕获IO异常
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
	}
}